package user

//annotation:user-service
//author:{"name":"user","tel":"18928776452","email":"XUnion@GMail.com"}

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/gomodule/redigo/redis"
	"io"
	"net/http"
	"sevlteServer/auto"
	"sevlteServer/tool"
)

func init() {
	//Setup package scope variables, just like logger, db connector, configure parameters, etc.
}

func Enroll(author string) {
	var developer *tool.ModuleAuthor
	if author != "" {
		var d tool.ModuleAuthor
		err := json.Unmarshal([]byte(author), &d)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		developer = &d
	}

	auto.AddService(&tool.ServeEndPoint{
		Fn: GetUsers,

		Path: "/users",
		Name: "users",

		Developer: developer,
	})

	auto.AddService(&tool.ServeEndPoint{
		Fn: DelUser,

		Path: "/deluser",
		Name: "deluser",

		Developer: developer,
	})

	//auto.AddService(&tool.ServeEndPoint{})
}

func user(w http.ResponseWriter, r *http.Request) {
	fmt.Println("user service")
	w.Write([]byte("hello"))
}

func GetUsers(r *http.Request, w http.ResponseWriter) {

	rd := tool.RsPool.Get()
	do, err := rd.Do("get", "GetUsers")
	if err != nil {
		fmt.Println(err.Error())
	}

	if do != nil {
		s, _ := redis.String(do, nil)
		//fmt.Println(s)
		_, err = w.Write([]byte(s))
		if err != nil {
			fmt.Println(err.Error())
			tool.Resp(w, &tool.ReplyProto{Status: -200})
			return
		}
		return
	}

	res, err := tool.DbPool.Query(context.Background(), "select account,nick,create_at from chat_user")
	if err != nil {
		fmt.Println(err.Error())
		tool.Resp(w, &tool.ReplyProto{Status: -200})
		return
	}

	type replyUsers struct {
		Chat_user []tool.Chat_user `json:"users"`
		Status    int              `json:"status"`
	}

	users := replyUsers{Status: 0}
	for res.Next() {
		var user tool.Chat_user
		err := res.Scan(&user.Account, &user.Nick, &user.CreateAt)

		if err != nil {
			fmt.Println(err.Error())
			return
		}
		users.Chat_user = append(users.Chat_user, user)
	}
	//fmt.Println(len(users.Chat_user))
	buf, err := json.Marshal(&users)
	if err != nil {
		fmt.Println(err.Error())
		tool.Resp(w, &tool.ReplyProto{Status: -200})
		return
	}
	do, err = rd.Do("set", "GetUsers", buf)
	_, err = w.Write(buf)
	if err != nil {
		fmt.Println(err.Error())
		tool.Resp(w, &tool.ReplyProto{Status: -200})
		return
	}
}

func DelUser(r *http.Request, w http.ResponseWriter) {
	body, err := io.ReadAll(r.Body)
	if err != nil {
		fmt.Println(err.Error())
		tool.Resp(w, &tool.ReplyProto{Status: -100})
		return
	}

	jsonMap := make(map[string]interface{})
	//将json字符串转换成map
	err = json.Unmarshal(body, &jsonMap)

	res, err := tool.DbPool.Exec(context.Background(), "delete from chat_user where account=$1", jsonMap["user"])
	if err != nil {
		fmt.Println(err.Error())
		tool.Resp(w, &tool.ReplyProto{Status: -200})
		return
	}

	fmt.Println(res.Delete(), res.RowsAffected())
	if res.Delete() && res.RowsAffected() == 1 {
		tool.Resp(w, &tool.ReplyProto{Status: 0})
		rd := tool.RsPool.Get()
		go rd.Do("del", "GetUsers")
		return
	}
	tool.Resp(w, &tool.ReplyProto{Status: -100})
}
